<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="用户编号" prop="personId">
        <el-input
          v-model="queryParams.personId"
          placeholder="请输入用户编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="合计金额" prop="allMoney">
        <el-input
          v-model="queryParams.allMoney"
          placeholder="请输入合计金额"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="订单状态" prop="orderType">
        <el-select
          v-model="queryParams.orderType"
          placeholder="订单状态"
          clearable
          size="small"
          style="width: 240px"
        >
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          :disabled="single"
          v-hasPermi="['fuw:order:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          v-hasPermi="['fuw:order:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['fuw:order:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['fuw:order:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="orderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="订单编号" align="center" prop="orderId" />
      <el-table-column label="用户编号" align="center" prop="personId" />
      <el-table-column label="合计金额" align="center" prop="allMoney" />
      <el-table-column label="订单生成时间" align="center" prop="orderStartTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.orderStartTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="订单结束时间" align="center" prop="orderEndTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.orderEndTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="订单状态" align="center" prop="orderType" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-document"
            @click="handleDetails(scope.row)"
            v-hasPermi="['fuw:order:details']"
          >详情</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改购物车对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1200px" append-to-body center>
      <el-table
        :data="commParam"
        :summary-method="getSummaries"
        show-summary
        default-expand-all
        highlight-current-row
        style="width: 100%">
        <el-table-column type="expand">
          <template slot-scope="props">
            <el-form label-position="left" inline class="demo-table-expand">
              <el-form-item label="商品分类:">
                <span>{{ props.row.commType }} </span>
              </el-form-item>
              <el-form-item label="">
                <span></span>
              </el-form-item>
              <el-form-item label="商品编号:">
                <span>{{ props.row.commId }}</span>
              </el-form-item>
              <el-form-item label="商品数量:">
                <span>{{ props.row.commKucun }} 件</span>
              </el-form-item>
              <el-form-item label="商品名称:">
                <span>{{ props.row.commName }}</span>
              </el-form-item>
              <el-form-item label="商品单价:">
                <span>{{ props.row.commDanjia }} 元</span>
              </el-form-item>
              <el-form-item label="总价格:">
                <span>{{ props.row.commPicture }} 元</span>
              </el-form-item>
              <el-form-item label="商品折扣:">
                <span>{{ props.row.commZhekou }} 折</span>
              </el-form-item>
            </el-form>
          </template>
        </el-table-column>
        <el-table-column
          label="商品名称"
          prop="commName">
        </el-table-column>
        <el-table-column
          label="商品折扣（折）"
          prop="commZhekou"
          width="180">
        </el-table-column>
        <el-table-column
          label="商品数量（件）"
          prop="commKucun"
          width="180">
        </el-table-column>
        <el-table-column
          label="商品单价（元）"
          prop="commDanjia"
          width="180">
        </el-table-column>
        <el-table-column
          label="总价格（元）"
          prop="commPicture"
          width="180">
        </el-table-column>
      </el-table>

      <el-steps :active="orderParam.orderType" finish-status="success" simple style="margin-top: 20px">
        <el-step title="订单创建" ></el-step>
        <el-step title="订单提交" ></el-step>
        <el-step title="订单失效" ></el-step>
      </el-steps>

    </el-dialog>
  </div>
</template>

<script>
import { listOrder, getOrder, delOrder, detailsOrder, addOrder, updateOrder, exportOrder } from "@/api/fuw/order";

export default {
  name: "Order",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 订单信息表格数据
      orderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 当前订单信息
      orderParam: [],
      // 商品信息详情
      commParam: [],
      // 订单状态数据字典
      statusOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        personId: null,
        allMoney: null,
        orderStartTime: null,
        orderEndTime: null,
        orderType: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        personId: [
          { required: true, message: "用户编号不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("fuw_order_type").then(response => {
      this.statusOptions = response.data;
    });
  },
  methods: {
    /** 查询订单信息列表 */
    getList() {
      this.loading = true;
      listOrder(this.queryParams).then(response => {
        this.orderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        orderId: null,
        personId: null,
        allMoney: null,
        orderStartTime: null,
        orderEndTime: null,
        orderType: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.orderId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 详情按钮操作 */
    handleDetails(row) {
      this.reset();
      this.orderParam = row;
      const orderId = row.orderId || this.ids
      detailsOrder(orderId).then(response => {
        this.commParam = response.data;
        this.open = true;
        this.title = "订单详情";
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const orderIds = row.orderId || this.ids;
      this.$confirm('是否确认删除订单信息编号为"' + orderIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delOrder(orderIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有订单信息数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportOrder(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    },
    /** 计算金额 **/
    getSummaries(param) {
      const { columns, data } = param;
      const sums = [];
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = '';
          return;
        }
        if (index === 1) {
          sums[index] = '总价';
          return;
        }
        if(index === 5){
          const values = data.map(item => Number(item[column.property]));
          if (!values.every(value => isNaN(value))) {
            sums[index] = values.reduce((prev, curr) => {
              const value = Number(curr);
              if (!isNaN(value)) {
                return prev + curr;
              } else {
                return prev;
              }
            }, 0);
            sums[index] += ' 元';
          }
        }else{
          sums[index] = '';
        }
      });
      return sums;
    }
  }
};
</script>

<style>
.demo-table-expand {
  font-size: 0;
}
.demo-table-expand label {
  width: 90px;
  color: #99a9bf;
}
.demo-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 50%;
}
</style>
